/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAO.UTIL.MySqlDataAccessHelper;
import POJO.DanhMuc;
import POJO.SanPham;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ThuyHuynh
 */
public class DAOSanPham {

    public static  ArrayList<SanPham> layDanhSachSanPham(String value) {
        ArrayList<SanPham> ds = new ArrayList<SanPham>();
        try {

            MySqlDataAccessHelper help = new MySqlDataAccessHelper();
            help.open();

            String str = "select * from sanpham where  TenSanPham like '%" + value + "%'";
            ResultSet rs = help.executeQuery(str);
            while (rs.next()) {
                try {
                    SanPham sp = new SanPham();
                    sp.setMaSP(rs.getInt("MaSanPham"));
                    sp.setMaDM(rs.getInt("MaDanhMuc"));
                    sp.setMaDD(rs.getInt("MaDiaDiem"));
                    sp.setMaTTSP(rs.getInt("MaTinhTrangSanPham"));
                    sp.setTenSP(rs.getString("TenSanPham"));;
                    sp.setChiTiet(rs.getString("ChiTiet"));
                    sp.setGiaGoc(rs.getInt("GiaGoc"));
                    sp.setTiLeGiam(rs.getInt("TiLeGiam"));
                    sp.setGiaGiam(rs.getInt("GiaGiam"));
                    sp.setPhiGiaoHang(rs.getInt("PhiGiaoHangTanNoi"));
                    sp.setNgayBD(rs.getDate("NgayGioBatDau"));
                    sp.setNgayKT(rs.getDate("NgayGioKetThuc"));
                    sp.setLuotXem(rs.getInt("LuotXem"));
                    sp.setLuotQC(rs.getInt("LuotQuangCao"));
                    sp.setDoHapDan(rs.getInt("DoHapDan"));
                    sp.setHinh(rs.getString("HinhAnh"));
                    sp.setPhieuToiThieu(rs.getInt("SLPhieuToiThieu"));
                    sp.setPhieuToiDa(rs.getInt("SLPhieuToiDa"));
                    sp.setLuotLoanTin(rs.getInt("LuotLoanTin"));
                    sp.setHSD(rs.getDate("HanSuDung"));
                    ds.add(sp);
                } catch (SQLException ex) {
                    Logger.getLogger(DAOSanPham.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            help.close();

        } catch (SQLException ex) {
            Logger.getLogger(DAODanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }

    public static SanPham laySanPhamTheoMa(int value) {
        SanPham sp = new SanPham();
        try {

            MySqlDataAccessHelper help = new MySqlDataAccessHelper();
            help.open();

            String str = "select * from sanpham where  masanpham= '" + value + "'";
            ResultSet rs = help.executeQuery(str);
            while (rs.next()) {
                try {
                    // SanPham sp  = new SanPham() ;
                    sp.setMaSP(rs.getInt("MaSanPham"));
                    sp.setMaDM(rs.getInt("MaDanhMuc"));
                    sp.setMaDD(rs.getInt("MaDiaDiem"));
                    sp.setMaTTSP(rs.getInt("MaTinhTrangSanPham"));
                    sp.setTenSP(rs.getString("TenSanPham"));;
                    sp.setChiTiet(rs.getString("ChiTiet"));
                    sp.setGiaGoc(rs.getInt("GiaGoc"));
                    sp.setTiLeGiam(rs.getInt("TiLeGiam"));
                    sp.setGiaGiam(rs.getInt("GiaGiam"));
                    sp.setPhiGiaoHang(rs.getInt("PhiGiaoHangTanNoi"));
                    sp.setNgayBD(rs.getDate("NgayGioBatDau"));
                    sp.setNgayKT(rs.getDate("NgayGioKetThuc"));
                    sp.setLuotXem(rs.getInt("LuotXem"));
                    sp.setLuotQC(rs.getInt("LuotQuangCao"));
                    sp.setDoHapDan(rs.getInt("DoHapDan"));
                    sp.setHinh(rs.getString("HinhAnh"));
                    sp.setPhieuToiThieu(rs.getInt("SLPhieuToiThieu"));
                    sp.setPhieuToiDa(rs.getInt("SLPhieuToiDa"));
                    sp.setLuotLoanTin(rs.getInt("LuotLoanTin"));
                    sp.setHSD(rs.getDate("HanSuDung"));
                    //ds.add(sp);   
                } catch (SQLException ex) {
                    Logger.getLogger(DAOSanPham.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            help.close();

        } catch (SQLException ex) {
            Logger.getLogger(DAODanhMuc.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.err.print("lay thanh cong");
        return sp;
    }


    public static ArrayList<SanPham> Lay30SanPham() {
        ArrayList<SanPham> ds = new ArrayList<SanPham>();
        try {

            MySqlDataAccessHelper help = new MySqlDataAccessHelper();
            help.open();

            String str = "select top(30) from sanpham";
            ResultSet rs = help.executeQuery(str);
            while (rs.next()) {
                try {
                    SanPham sp = new SanPham();
                    sp.setMaSP(rs.getInt("MaSanPham"));
                    sp.setMaDM(rs.getInt("MaDanhMuc"));
                    sp.setMaDD(rs.getInt("MaDiaDiem"));
                    sp.setMaTTSP(rs.getInt("MaTinhTrangSanPham"));
                    sp.setTenSP(rs.getString("TenSanPham"));;
                    sp.setChiTiet(rs.getString("ChiTiet"));
                    sp.setGiaGoc(rs.getInt("GiaGoc"));
                    sp.setTiLeGiam(rs.getInt("TiLeGiam"));
                    sp.setGiaGiam(rs.getInt("GiaGiam"));
                    sp.setPhiGiaoHang(rs.getInt("PhiGiaoHangTanNoi"));
                    sp.setNgayBD(rs.getDate("NgayGioBatDau"));
                    sp.setNgayKT(rs.getDate("NgayGioKetThuc"));
                    sp.setLuotXem(rs.getInt("LuotXem"));
                    sp.setLuotQC(rs.getInt("LuotQuangCao"));
                    sp.setDoHapDan(rs.getInt("DoHapDan"));
                    sp.setHinh(rs.getString("HinhAnh"));
                    sp.setPhieuToiThieu(rs.getInt("SLPhieuToiThieu"));
                    sp.setPhieuToiDa(rs.getInt("SLPhieuToiDa"));
                    sp.setLuotLoanTin(rs.getInt("LuotLoanTin"));
                    sp.setHSD(rs.getDate("HanSuDung"));
                    ds.add(sp);

                } catch (SQLException ex) {
                    Logger.getLogger(DAOSanPham.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            help.close();

        } catch (SQLException ex) {
            Logger.getLogger(DAOSanPham.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }
    public static ArrayList<SanPham> LayDanhSachSanPham() {
        ArrayList<SanPham> ds = new ArrayList<SanPham>();
        try {

            MySqlDataAccessHelper help = new MySqlDataAccessHelper();
            help.open();

            String str = "select * from sanpham";
            ResultSet rs = help.executeQuery(str);
            while (rs.next()) {
                try {
                    SanPham sp = new SanPham();
                    sp.setMaSP(rs.getInt("MaSanPham"));
                    sp.setMaDM(rs.getInt("MaDanhMuc"));
                    sp.setMaDD(rs.getInt("MaDiaDiem"));
                    sp.setMaTTSP(rs.getInt("MaTinhTrangSanPham"));
                    sp.setTenSP(rs.getString("TenSanPham"));;
                    sp.setChiTiet(rs.getString("ChiTiet"));
                    sp.setGiaGoc(rs.getInt("GiaGoc"));
                    sp.setTiLeGiam(rs.getInt("TiLeGiam"));
                    sp.setGiaGiam(rs.getInt("GiaGiam"));
                    sp.setPhiGiaoHang(rs.getInt("PhiGiaoHangTanNoi"));
                    sp.setNgayBD(rs.getDate("NgayGioBatDau"));
                    sp.setNgayKT(rs.getDate("NgayGioKetThuc"));
                    sp.setLuotXem(rs.getInt("LuotXem"));
                    sp.setLuotQC(rs.getInt("LuotQuangCao"));
                    sp.setDoHapDan(rs.getInt("DoHapDan"));
                    sp.setHinh(rs.getString("HinhAnh"));
                    sp.setPhieuToiThieu(rs.getInt("SLPhieuToiThieu"));
                    sp.setPhieuToiDa(rs.getInt("SLPhieuToiDa"));
                    sp.setLuotLoanTin(rs.getInt("LuotLoanTin"));
                    sp.setHSD(rs.getDate("HanSuDung"));
                    ds.add(sp);
                } catch (SQLException ex) {
                    Logger.getLogger(DAOSanPham.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            help.close();

        } catch (SQLException ex) {
            Logger.getLogger(DAOSanPham.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }
}
